<template>
  <view>
  <view class="search-box" >
   <uni-search-bar @input="input" :radius="100" cancelButton='none' :focus="true"></uni-search-bar>
  </view>
  <!-- 搜索建议列表 -->
  <view class="sugg-list" v-if="searchResults.length!=0">
    <view class="sugg-item" v-for="(item ,i) in searchResults" :key="i" @click="gotoDetails(item)">
      <view class="goods-name">{{item.goods_name}}</view>
      <uni-icons type="right" size="16"></uni-icons>
    </view>
  </view>
  <!-- 搜索历史 -->
  <view class="history-box" v-else>
    <view class="history-title">
      <text>搜索历史</text>
      <uni-icons type="trash" size="17" @click="clean"></uni-icons>
    </view>
    <view class="history-list">
      <uni-tag :text="item" v-for="( item,i ) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
    </view>
  </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
            // 延时器的 timerId
            timer: null,
            // 搜索关键词
            kw: '',
            searchResults:[],
            historyList:[]
      };
    },
    onLoad() {
       this.historyList=JSON.parse(uni.getStorageSync('hL')||'[]')
    },
      computed:{
        histories(){
          return [...this.historyList].reverse()
        }
      },
    methods:{
      input(e) {
        // 清除 timer 对应的延时器
        clearTimeout(this.timer)
        // 重新启动一个延时器，并把 timerId 赋值给 this.timer
        this.timer = setTimeout(() => {
          // 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
          this.kw = e
          // console.log(this.kw)
          this.getsearchResults()
        }, 500)
      },
      gotoGoodsList(kw){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+kw
        })      },
      clean(){
        this.historyList=[]
        uni.setStorageSync('hL','[]')
      },
      async getsearchResults(){
        if(this.kw==''){
          this.searchResults=[]
          return
        }
      const {data:res}=await uni.$http.get('/api/public/v1/goods/qsearch',{ query:this.kw})
      if(res.meta.status!=200) return uni.$showMsg()
       this.searchResults=res.message
       this.saveSearchHistory()
      },
      saveSearchHistory(){
        // this.historyList.push(this.kw)
        // 1.将Array数组转化为Set对象(因为set对象里不会出现重复元素)
        const set=new Set(this.historyList)
        // 2.调用Set对象的delete方法,移除对应的元素,再调用Set对象的add方法,向Set中添加元素(这样可以使以前出现的元素删除,并目前出现的元素排在最前面)
        set.delete(this.kw)
        set.add(this.kw)
        // 3.将Set对象转化为Array数组(将类型复原)
        this.historyList=Array.from(set)
        // 将搜索记录持久化处理
        uni.setStorageSync('hL',JSON.stringify(this.historyList))
      },
      gotoDetails(item){
        uni.navigateTo({
          url :'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top:0;
  z-index: 999;
  }
  .sugg-list{
    padding: 0 5px;
    .sugg-item{
      font-size:12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .goods-name{
        // 文字不允许换行(单行文本)
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后,使用...代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
  .history-box{
    padding: 0 5px;
    .history-title{
      display: flex;
      justify-content: space-between;
      height: 40px;
      align-items: center;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }
    .history-list{
      display: flex;
      flex-wrap: wrap;
      .uni-tag{
        margin-top: 5px;
        margin-right: 5px;
        background-color: beige;
        color: #000;
      }
    }
    
  }
</style>
